<?php
/*
 * RPSessionManager.php created on 28 avr. 08 
 * Author: Rodrigue Le Gall
 *
 * RPortal is an open source web based Content Management System
 * Copyright (C) 2002-2008  Rodrigue Le Gall
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * Implementation of RPSessionInterface
 * It is the default Session manager in RPortal
 * @RPComponent
 */
class RPSessionManager implements RPSessionManagerInterface {

	var $session;

	/**
	 * constructor of RPSessionManager
	 * 
	 * during the instantiation of the session manager, the right session is created.
	 * @return RPSessionManager a new instance of RPSessionManager
	 */
	function __construct() {
		if (isset ($_SESSION['RPSession'])) {
			$this->session = new $_SESSION['RPSessionClass'] ();
			$this->session->setAllData($_SESSION['RPSession']);
			if ($this->session->isValid()) {
				return;
			}
			$this->session = null;
		}
		$tSession = 'RPSessionManager';
		while ($tSession != null) {
			$tSession2 = RPortal :: getClass('RPSessionInterface', $tSession);
			if ($tSession2 == $tSession)
				break;
			$tSession = $tSession2;
			if ($tSession == null) {
				break;
			}
			$t = new $tSession ();
			if ($t->isValid()) {
				$this->session = $t;
				$this->save();
				return;
			}
		}
		throw new Exception("pas de session valide");
	}

	public function save() {
		$_SESSION['RPSession'] = $this->session->getAllData();
		$_SESSION['RPSessionClass'] = get_class($this->session);
	}
	
	public function getSession(){
		return $this->session;
	}
}
